package br.com.trumah.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import br.com.trumah.model.AssistenteLogistica;
import br.com.trumah.model.AssistenteSocial;
import br.com.trumah.model.Atendente;
import br.com.trumah.model.Beneficiario;
import br.com.trumah.model.Beneficio;
import br.com.trumah.model.Cidade;
import br.com.trumah.model.Coordenadas;
import br.com.trumah.model.Dependente;
import br.com.trumah.model.Doacao;
import br.com.trumah.model.Doador;
import br.com.trumah.model.DoadorFisico;
import br.com.trumah.model.DoadorJuridico;
import br.com.trumah.model.Endereco;
import br.com.trumah.model.Estado;
import br.com.trumah.model.Funcionario;
import br.com.trumah.model.Perfil;
import br.com.trumah.model.Pessoa;
import br.com.trumah.model.Rota;
import br.com.trumah.model.Usuario;

public class HibernateUtil {
	
	private static SessionFactory sessionFactory;
	
	private HibernateUtil(){
		
	}
	
	public static SessionFactory getSessionFactory(){
		
		if (sessionFactory == null){
			try{
				
				AnnotationConfiguration ac = new AnnotationConfiguration();
				ac.addAnnotatedClass(Perfil.class);
				ac.addAnnotatedClass(AssistenteLogistica.class);
				ac.addAnnotatedClass(AssistenteSocial.class);
				ac.addAnnotatedClass(Beneficiario.class);
				ac.addAnnotatedClass(Beneficio.class);
				ac.addAnnotatedClass(Cidade.class);
				ac.addAnnotatedClass(Coordenadas.class);
				ac.addAnnotatedClass(Estado.class);
				ac.addAnnotatedClass(Endereco.class);
				ac.addAnnotatedClass(Dependente.class);
				ac.addAnnotatedClass(Doacao.class);
				ac.addAnnotatedClass(Doador.class);
				ac.addAnnotatedClass(DoadorFisico.class);
				ac.addAnnotatedClass(DoadorJuridico.class);
				ac.addAnnotatedClass(Funcionario.class);
				ac.addAnnotatedClass(Pessoa.class);
				ac.addAnnotatedClass(Atendente.class);
				ac.addAnnotatedClass(Rota.class);
				ac.addAnnotatedClass(Usuario.class);
				sessionFactory = ac.configure().buildSessionFactory();
				
			} catch (Throwable ex){
				
				System.err.println("Criacao inicial da SessionFactory falhou! "+ ex);
				throw new ExceptionInInitializerError(ex);
				
			}
			
			return sessionFactory;
			
		} else {
			
			return sessionFactory;
			
		}
	}

}
